package zhieasy.cn.xpu_helper.controller.found;

import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;
import zhieasy.cn.common.utils.ResultVOUtil;
import zhieasy.cn.common.vo.ResultVO;
import zhieasy.cn.model.common.StudentUser;
import zhieasy.cn.model.common.WechatUser;
import zhieasy.cn.model.found.LostAndFind;
import zhieasy.cn.model.found.Opinion;
import zhieasy.cn.service.found.UserService;

/**
 * @author wangzeya
 * @create 2020-08-02 21:36:00
 */
@RestController
@RequestMapping("/found")
@Api(tags = "失物招领模块，与个人信息相关接口")
@ApiImplicitParams({
        @ApiImplicitParam(name="opinion",value ="反馈意见",required =true,paramType="form",dataType="String"),
        @ApiImplicitParam(name="pageNum",value ="当前页码",required =true,paramType="form",dataType="int"),
        @ApiImplicitParam(name="pageSize",value ="页面大小",required =true,paramType="form",dataType="int")
})
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 查看我发布的失物招领信息
     * @return
     */
    @ApiOperation(value = "查看我发布的失物招领信息", notes = "查看我发布的失物招领信息")
    @PostMapping(value = "/lostAndFound/user/{openId}"
        , produces = "application/json;charset=UTF-8")
    public ResultVO<LostAndFind> findMyLaf(@ApiParam("页数")@RequestParam("pageIndex") int pageIndex,
                                           @ApiParam("每页要多少数据")@RequestParam("pageSize")int pageSize,
                                           @ApiParam("openId")@PathVariable("getOpenId")String getOpenId) {
        return userService.findMyLaf(pageIndex, pageSize, getOpenId);
    }

    /**
     * 学生认证
     *
     * @param studentUser
     * @return
     */
    @ApiIgnore
    //@ApiOperation(value = "学生认证", notes = "学生认证")
    @PostMapping(value = "/stuCert", produces = "application/json;charset=UTF-8")
    public ResultVO createStuCert(@RequestBody StudentUser studentUser) {
        studentUser.setCreateTime(System.currentTimeMillis());
        ResultVO resultVO = null;
        //第一次认证
        if (studentUser.getUpdateTime() == null) {
            resultVO = userService.createStuCert(studentUser);
        } else {
            resultVO = userService.modifyStuCertification(studentUser);
        }
        return resultVO;
    }

    /**
     * 发送用户反馈信息
     *
     * @param opinion
     * @return
     */
    @ApiIgnore
    //@ApiOperation(value = "发送用户反馈信息", notes = "发送用户反馈信息")
    @PostMapping(value = "/opinion", produces = "application/json;charset=UTF-8")
    public ResultVO createOpinion(@RequestBody Opinion opinion) {
        return userService.createOpinion(opinion);
    }

    /**
     * 完善用户信息
     *
     * @param wechatUser
     * @return
     */
    @ApiIgnore
    //@ApiOperation(value = "完善用户信息", notes = "完善用户信息")
    @PostMapping(value = "/completeuser", produces = "application/json;charset=UTF-8")
    public ResultVO completeUser(@RequestBody WechatUser wechatUser) {
        ResultVO resultVO = new ResultVO<>();
        //判断是否第一进行信息完善
        if (wechatUser.getUpdateTime() == null) {
            //如果是第一次 则正常加入  wechatUser 中其实只有 电话和邮箱  修改次数三个值
            resultVO = userService.completeUser(wechatUser);
        } else {
            //不是第一次则修改
            resultVO = userService.modifyWeChatUser(wechatUser);
        }
        return resultVO;

    }

    /**
     * 点击完成发布的失物招领信息
     *
     * @param lafId
     * @return
     */

    @ApiIgnore
//    @ApiOperation(value = "点击完成发布的失物招领信息", notes = "点击完成发布的失物招领信息")
    @PostMapping(value = "/complete", produces = "application/json;charset=UTF-8")
    public ResultVO completeLaf(@RequestParam("laf_id") String lafId) {
        return userService.completeLaf(lafId);
    }

    /**
     * 查看用户信息是否完成
     *
     * @param userId
     * @return
     */
    @ApiIgnore
    //@ApiOperation(value = "查看用户信息是否完成", notes = "查看用户信息是否完成")
    @GetMapping(value = "/iscompleteuser", produces = "application/json;charset=UTF-8")
    public ResultVO iscompleteuser(@RequestParam("user_id") String userId) {
        JSONObject data = new JSONObject();
        int competeUser = userService.isCompeteUser(userId);
        ResultVO<WechatUser> resultVO = userService.findUserByUserId(userId);
        WechatUser wechatUser = resultVO.getData();
        if (wechatUser == null) {
            data.put("user_profile", "");
        } else {
            data.put("user_profile", wechatUser);
        }
        data.put("is_complete", competeUser);
        return ResultVOUtil.success(data);
    }

    /**
     * 查看是否进行学生认证
     *
     * @param userId
     * @return
     */
    @ApiIgnore

//    @ApiOperation(value = "查看是否进行学生认证", notes = "查看是否进行学生认证")
    @GetMapping(value = "/isstucert", produces = "application/json;charset=UTF-8")
    public ResultVO isStuCert(@RequestParam("user_id") String userId) {
        JSONObject data = new JSONObject();
        int isStuCert = userService.isStuCert(userId);
        ResultVO<StudentUser> resultVO = userService.findStuByUserId(userId);
        StudentUser stuCert = resultVO.getData();
        if (stuCert == null) {
            data.put("stu_cert", "");
        } else {
            data.put("stu_cert", stuCert);
        }
        data.put("stu_verified", isStuCert);
        return ResultVOUtil.success(data);
    }

}

